package org.dromara.web.controller;


import cn.dev33.satoken.annotation.SaIgnore;


import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.TokenStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.tool.NumberHandler;
import org.dromara.web.mapper.MarketMapper;
import org.dromara.web.service.IChatAssistant;
import org.dromara.web.service.impl.ApiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Flux;

@SaIgnore
@RestController
@RequiredArgsConstructor
public class ChatController {

    private final IChatAssistant chatAssistant;
    private final ChatModel chatModel;
    private final StreamingChatModel streamingChatModel;
    private final RestTemplate restTemplate;
    private final MarketMapper marketMapper;
    private final ObjectMapper objectMapper;
    //
//
//    @GetMapping("/lowChat")
//    public String lowChat(@RequestParam("message") String message) {
//        // 构建带系统指令的消息链
//        SystemMessage systemMsg = SystemMessage.from("你是一个新能源汽车专家，用中文回答，分点说明优势，每点附带实际数据");
//        UserMessage userMsg = UserMessage.from(message); // 使用传入的message参数
//        AiMessage aiMessage = chatLanguageModel.chat(systemMsg, userMsg).aiMessage();
//
//        return aiMessage.text();
//    }
//
//    @GetMapping("/highChat")
//    public String highChat(@RequestParam("message") String message) {
//        return AiServices.create(EnergyCarAssistant.class, chatLanguageModel).explainAdvantages(message);
//    }

    //    /**
//     * 聊天
//     *
//     * @param message
//     * @return
//     */
//    @GetMapping("/ChatAssistant")
//    public String chat3(String message) {
//        return chatAssistant.chat(message);
////        return AiServices.create(chatAssistant, chatLanguageModel).chat(message);
//    }
// 不推荐的做法，仅作为示例
//    @GetMapping("/ChatAssistant")
//    public String chat3(String message) {
//        return AiServices.builder(IChatAssistant.class)
//            .chatLanguageModel(chatLanguageModel)
//            .tools(new ApiController(new ApiServiceImpl(restTemplate, marketMapper, objectMapper)))
//            .build()
//            .chat(message);
//    }
    @GetMapping("/ChatAssistant")
    public String chat3(String message) {
        return AiServices.builder(IChatAssistant.class)
            .chatModel(chatModel)
            .tools(new ApiController(new ApiServiceImpl(restTemplate, marketMapper, objectMapper)))
            .build()
            .chat(message);
    }

    @GetMapping("/streamChat")
    public Flux<String> streamChat(@RequestParam("message") String prompt,
                                   HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        return Flux.create(sink -> {
            streamingChatModel.chat(prompt, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {
                    System.out.println(s);
                    sink.next(s);
                }

                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    sink.complete();
                }

                @Override
                public void onError(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }
            });
        });
    }


    @GetMapping("/ChatAssistant2")
    public Flux<String> chat4(String message, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        return AiServices.builder(IChatAssistant.class)
            .streamingChatModel(streamingChatModel)
            .tools(new ApiController(new ApiServiceImpl(restTemplate, marketMapper, objectMapper)))
            .build()
            .chat2(message);
    }
}
